package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.dao.ExpressFeeRuleDAO;
import com.bsoft.gol.hbs.dao.ExpressModeRuleDAO;
import com.bsoft.gol.hbs.dao.ExpressSectionRuleDAO;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.feerule.ExpressFeeRule;
import hcn.base.feerule.ExpressModeRule;
import hcn.base.feerule.ExpressSectionRule;
import hcn.service.feerule.ExpressFeeRuleService;
import hcn.service.feerule.qo.ExpressFeeRuleQo;
import hcn.service.feerule.qo.ExpressModeRuleQo;
import hcn.service.feerule.vo.ExpressRuleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 计费规则模板
 *
 * @author sumu
 */
@SsdevService("expressFeeRuleService")
public class ExpressFeeRuleServiceImpl implements ExpressFeeRuleService {

    public static final String UNIT_WEIGHT = "1";

    public static final String UNIT_NUM = "2";

    private static Logger logger = LoggerFactory.getLogger(ExpressFeeRuleServiceImpl.class);

    @Autowired
    private ExpressFeeRuleDAO expressFeeRuleDAO;
    @Autowired
    private ExpressModeRuleDAO expressModeRuleDAO;
    @Autowired
    private ExpressSectionRuleDAO expressSectionRuleDAO;

    @Override
    @RpcService
    public String save(ExpressFeeRule bean) {
        if(CommonUtils.isEmpty(bean.getRuleId())){
            bean.setRuleId(UUID.randomUUID().toString());
            expressFeeRuleDAO.save(bean);
        }else{
            ExpressFeeRule expressFeeRule_new = expressFeeRuleDAO.get(bean.getRuleId());
            if(CommonUtils.isNotEmpty(expressFeeRule_new)){
                expressFeeRuleDAO.update(bean);
            }
        }
        return bean.getRuleId();
    }





    @Override
    @RpcService
    public List<ExpressFeeRule> findExpressFeeRuleList() {
        return expressFeeRuleDAO.findExpressFeeRuleList();
    }

    @Override
    @RpcService
    public void deleteById(String id) {
        expressFeeRuleDAO.remove(id);
        List<String> moduleIds = new ArrayList<>();
        List<ExpressModeRule> expressModeRules = expressModeRuleDAO.findModeByRuleId(id);
        if(CommonUtils.isNotEmpty(expressModeRules)){
            for (ExpressModeRule expressModeRule:expressModeRules) {
                moduleIds.add(expressModeRule.getModeId());
                expressModeRuleDAO.remove(expressModeRule.getModeId());
            }
        }
        if(CommonUtils.isNotEmpty(moduleIds)){
            List<ExpressSectionRule> expressSectionRules = expressSectionRuleDAO.findSectionRule(moduleIds);
            for (ExpressSectionRule expressSectionRule:expressSectionRules) {
                expressSectionRuleDAO.remove(expressSectionRule.getId());
            }
        }
    }


    @Override
    @RpcService
    public List<ExpressRuleVo> findModeDetail(String ruleId) {
        ExpressFeeRule one = expressFeeRuleDAO.get(ruleId);

        if (CommonUtils.isNotEmpty(one)) {

            List<ExpressModeRule> list = expressModeRuleDAO.findModeByRuleId(one.getRuleId());
            if (CommonUtils.isNotEmpty(list)) {

                List<ExpressRuleVo> vo = new ArrayList<>(list.size());

                List<String> modeIds = new ArrayList<>();
                if (UNIT_WEIGHT.equals(one.getRuleType())) {
                    // 计重
                    for (ExpressModeRule rule : list) {
                        ExpressRuleVo expressRuleVo = new ExpressRuleVo();
                        BeanUtils.copyProperties(rule, expressRuleVo);
                        vo.add(expressRuleVo);
                    }
                    return vo;
                }

                if (UNIT_NUM.equals(one.getRuleType())) {
                    // 计件
                    for (ExpressModeRule rule : list) {
                        ExpressRuleVo expressRuleVo = new ExpressRuleVo();
                        BeanUtils.copyProperties(rule, expressRuleVo);
                        modeIds.add(rule.getModeId());
                        vo.add(expressRuleVo);
                    }

                    List<ExpressSectionRule> sectionRules = expressSectionRuleDAO.findSectionRule(modeIds);

                    Map<String, List<ExpressSectionRule>> map = new HashMap<>();
                    if (CommonUtils.isNotEmpty(sectionRules)) {
                        for (ExpressSectionRule rule : sectionRules) {
                            if (map.containsKey(rule.getModeId())) {
                                List<ExpressSectionRule> sectionRules1 = map.get(rule.getModeId());
                                sectionRules1.add(rule);
                            } else {
                                List<ExpressSectionRule> sectionRules1 = new ArrayList<>();
                                sectionRules1.add(rule);
                                map.put(rule.getModeId(), sectionRules1);
                            }
                        }

                        for (ExpressRuleVo ruleVo : vo) {
                            if (map.containsKey(ruleVo.getModeId())) {
                                ruleVo.setSectionRules(map.get(ruleVo.getModeId()));
                            }
                        }
                    }

                    return vo;

                }
            }
        }
        return null;
    }

//    @Override
//    public ExpressModeRule findRuleDetail(ExpressModeRule expressModeRule) {
//        ExpressModeRule expressModeRule1 = null;
//        List<ExpressModeRule> expressModeRules = service.findModeByRuleId(expressModeRule);
//        if (CommonUtils.isNotEmpty(expressModeRules)) {
//            if ("1".equals(expressModeRule.getRuleType())) {
//                expressModeRule1 = expressModeRules.get(0);
//            }else {
//                expressModeRule1 = expressModeRules.get(0);
//                ExpressSectionRule expressSectionRule = new ExpressSectionRule();
//                expressSectionRule.setModeId(expressModeRule1.getModeId());
//                expressSectionRule.setPiece(expressModeRule.getPiece());
//                List<ExpressSectionRule> sectionRules = service.findSectionRule(expressSectionRule);
//                if (CommonUtils.isNotEmpty(sectionRules)) {
//                    expressModeRule1.setExpressSectionRules(sectionRules.get(0));
//                }
//            }
//        }
//        return expressModeRule1;
//    }

    @Override
    @RpcService
    public void saveModeRule(ExpressFeeRuleQo qo) {
        if (CommonUtils.isNotEmpty(qo.getRuleId())) {
            ExpressFeeRule expressFeeRule = new ExpressFeeRule();
            BeanUtils.copyProperties(qo, expressFeeRule);
            this.save(expressFeeRule);

            List<ExpressModeRuleQo> modeRules = qo.getModeRules();
            if (CommonUtils.isNotEmpty(modeRules)) {
                for (ExpressModeRuleQo modeRuleQo : modeRules) {
                    //解决前端ruleId没传导致的问题
                    modeRuleQo.setRuleId(expressFeeRule.getRuleId());

                    ExpressModeRule modeRule = new ExpressModeRule();

                    BeanUtils.copyProperties(modeRuleQo, modeRule);

                    if (CommonUtils.isNotEmpty(modeRule.getModeId())) {
                        expressModeRuleDAO.update(modeRule);

                        if (UNIT_NUM.equals(qo.getRuleType())) {
                            List<ExpressSectionRule> sectionRules = modeRuleQo.getSectionRules();
                            if (CommonUtils.isNotEmpty(sectionRules)) {
                                for (ExpressSectionRule rule : sectionRules) {
                                    if (CommonUtils.isNotEmpty(rule.getId())) {
                                        expressSectionRuleDAO.update(rule);
                                    } else {
                                        rule.setModeId(modeRule.getModeId());
                                        rule.setId(UUID.randomUUID().toString());
                                        expressSectionRuleDAO.save(rule);
                                    }
                                }
                            }
                        }
                    } else {
                        String modeId = UUID.randomUUID().toString();

                        modeRule.setModeId(modeId);

                        expressModeRuleDAO.save(modeRule);

                        if (UNIT_NUM.equals(qo.getRuleType())) {
                            saveSectionRule(modeRuleQo, modeId);
                        }
                    }

                }
            }

        } else {
            ExpressFeeRule expressFeeRule = new ExpressFeeRule();
            BeanUtils.copyProperties(qo, expressFeeRule);



            String ruleId = this.save(expressFeeRule);

            List<ExpressModeRuleQo> modeRules = qo.getModeRules();

            if (CommonUtils.isNotEmpty(modeRules)) {
                for (ExpressModeRuleQo modeRuleQo : modeRules) {
                    ExpressModeRule modeRule = new ExpressModeRule();

                    BeanUtils.copyProperties(modeRuleQo, modeRule);

                    modeRule.setRuleId(ruleId);

                    String modeId = UUID.randomUUID().toString();

                    modeRule.setModeId(modeId);

                    expressModeRuleDAO.save(modeRule);

                    if (UNIT_NUM.equals(qo.getRuleType())) {
                        saveSectionRule(modeRuleQo, modeId);
                    }
                }
            }
        }
    }

    private void saveSectionRule(ExpressModeRuleQo modeRuleQo, String modeId) {
        List<ExpressSectionRule> sectionRules = modeRuleQo.getSectionRules();
        if (CommonUtils.isNotEmpty(sectionRules)) {

            for (ExpressSectionRule rule : sectionRules) {
                rule.setModeId(modeId);
                rule.setId( UUID.randomUUID().toString());

                expressSectionRuleDAO.save(rule);
            }
        }
    }
}